package com.cade.agriculturemall.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewTreeObserver
import android.widget.RelativeLayout
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.concurrent.TimeUnit
import android.widget.LinearLayout
import com.cade.agriculturemall.R
import com.eyevision.common.widget.ProgressAnimation


/**
 *
 * 打开默认loading状态
 * Created by tom on 17/9/1.
 */
class EmptyLayout(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet), View.OnClickListener {


    private val progressAnimation by lazy {
        findViewById(R.id.pa) as ProgressAnimation
    }

    private val emptyRl by lazy {
        findViewById(R.id.empty_rl) as RelativeLayout
    }

    private val emptyLL by lazy {
        findViewById(R.id.empty_ll) as LinearLayout
    }

    var onEmptyLayoutListener: OnEmptyLayoutListener? = null

    val contentView by lazy {
        val count = childCount
        val tag = context.resources.getString(R.string.empty_tag)
        val child: View? = (0..count - 1)
                .map { getChildAt(it) }
                .first {
                    it.tag == null || it.tag.toString() != tag
                }
        child
    }

    private var isLoading = false

    init {
        LayoutInflater.from(context).inflate(R.layout.empty_layout, this@EmptyLayout, true)
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                contentView?.visibility = View.GONE
                progressAnimation.bringToFront()
                emptyRl.bringToFront()
                emptyLL.setOnClickListener(this@EmptyLayout)
                loading()
                viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }


    /**
     * 检查List的数据以及是否Null
     * 自动设置状态
     * */
    fun changeStatusWithList(list: List<Any>?) {

        if (list == null || list.isEmpty()) {
            empty()
        } else {
            reset()
        }
    }

    override fun onClick(v: View?) {
        onEmptyLayoutListener?.let {
            if (it.onReloadClick(this)) {
                loading()
            }
        }

    }

    /**
     * 设置loading状态
     * */
    fun loading() {
        if (!isLoading) {
            isLoading = true
            contentView?.visibility = View.GONE
            progressAnimation.visibility = View.VISIBLE
            emptyRl.visibility = View.VISIBLE
            emptyLL.visibility = View.GONE
            progressAnimation.start()
        }
        /*Observable.timer(100, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {

                }
                */
    }

    /**
     * 设置空页面状态
     * */
    fun empty() {
        Observable.timer(300, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    isLoading = false
                    contentView?.visibility = View.GONE
                    emptyRl.bringToFront()
                    emptyRl.visibility = View.VISIBLE
                    progressAnimation.visibility = View.GONE
                    emptyLL.visibility = View.VISIBLE
                    emptyLL.bringToFront()
                    progressAnimation.clear()
                }

    }

    /**
     * 重置状态展示内容页
     * */
    fun reset() {
        Observable.timer(300, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    isLoading = false
                    contentView?.visibility = View.VISIBLE
                    emptyRl.visibility = View.GONE
                    progressAnimation.visibility = View.GONE
                    emptyLL.visibility = View.GONE
                    progressAnimation.clear()
                }
    }

    public interface OnEmptyLayoutListener {
        fun onReloadClick(layout: EmptyLayout): Boolean
    }


}